﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MegavaniaX.World;

namespace MegavaniaX.Actors.States
{
    public class PlayerStateSlide : PlayerStateBase
    {
        private float _initialVelocityX;
        private DateTime startTime; // time slide was started

        private static MegavaniaEventType[] _eventsAllowed = new MegavaniaEventType[]
        {
            MegavaniaEventType.JumpPressed,
            MegavaniaEventType.MoveRightPressed,
            MegavaniaEventType.MoveRightReleased,
            MegavaniaEventType.MoveLeftPressed,
            MegavaniaEventType.MoveLeftReleased
        };      

        public PlayerStateSlide(Player player, float initialXVelo)
            : base(player)
        {
            _initialVelocityX = initialXVelo;
            startTime = DateTime.Now;
            float curBottom = _player.Bottom;
            _player.Height = Player.SlideHeight;
            _player.Bottom = curBottom;
        }


        public override void OnEnter()
        {
            _player.velocity.X = _initialVelocityX;
            if (_initialVelocityX > 0.0f)
            {
                _player.IsFacingLeft = false;
            }
            else
            {
                _player.IsFacingLeft = true;
            }
            _player.CurrentAnimation = _player.animationSlide;
        }

        public override bool Update(TimeSpan elapsedTime, Screen screen)
        {
            if (base.Update(elapsedTime, screen))
                return true;

            if (!_player.IsActorOnSolidGround(screen))
            {
                NextState = new PlayerStateJump(_player, 0.0f);
                if (_player.velocity.X > 0)
                    _player.velocity.X = Player.InitialXVelo;
                else
                    _player.velocity.X = -Player.InitialXVelo;
                return true;
            }

            if (DateTime.Now - startTime > Player.SlideDuration)
            {
                if (_player.velocity.X > 0)
                    NextState = new PlayerStateMove(_player, Player.InitialXVelo);
                else
                    NextState = new PlayerStateMove(_player, -Player.InitialXVelo);
                return true;
            }

            return false;

        }

        public override bool CanHandleEvent(MegavaniaEventType evtType)
        {
            return _eventsAllowed.Contains(evtType);
        }

        public override bool HandleEvent(IMegavaniaEvent evt)
        {
            if (evt.EventType == MegavaniaEventType.JumpPressed)
            {
                NextState = new PlayerStateJump(_player, Player.InitialYVelo);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.MoveRightPressed && _player.velocity.X < 0)
            {
                NextState = new PlayerStateMove(_player, Player.InitialXVelo);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.MoveLeftPressed && _player.velocity.X > 0)
            {
                NextState = new PlayerStateMove(_player, -Player.InitialXVelo);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.MoveLeftReleased || evt.EventType == MegavaniaEventType.MoveRightReleased)
            {
                NextState = new PlayerStateIdle(_player);
                _player.velocity.X = 0.0f;
                return true;
            }


            return false;
        }

        public override void StopShootingAnimation()
        {
            _player.CurrentAnimation = _player.animationJump;
        }

        public override void OnExit()
        {
            float _curBottom = _player.Bottom;
            _player.Height = Player.NormalHeight;
            _player.Bottom = _curBottom;
        }
    }
}
